#include "tire.h"

#include <GL/glut.h>

#include <cmath>
#include <iostream>

cTire::cTire(int x, int y) {
	m_xpos = x;
	m_ypos = y;
	m_angle = 0;
	m_tireDiameter = 100;
	m_tireWidth = 50;
	m_matrixAngle = 0;
}

cTire::~cTire(){
}

//rotating around the origin
void cTire::rotateMatrix(int degrees) {
	m_matrixAngle = degrees;
	double tmpx; double tmpy;
	//shouldn't be needed because it's at the origin
	tmpx = m_leftMiddleVert.x * cosinus(m_matrixAngle) - m_leftMiddleVert.y * sinus(m_matrixAngle);
	tmpy = m_leftMiddleVert.y * cosinus(m_matrixAngle) + m_leftMiddleVert.x * sinus(m_matrixAngle);
	
	m_leftMiddleVert.x = tmpx;
	m_leftMiddleVert.y = tmpy;

	tmpx = m_leftFrontVert.x * cosinus(m_matrixAngle) - m_leftFrontVert.y * sinus(m_matrixAngle);
	tmpy = m_leftFrontVert.y * cosinus(m_matrixAngle) + m_leftFrontVert.x * sinus(m_matrixAngle);

	m_leftFrontVert.x = tmpx; 
	m_leftFrontVert.y = tmpy;
	
	tmpx = m_rightFrontVert.x * cosinus(m_matrixAngle) - m_rightFrontVert.y * sinus(m_matrixAngle);
	tmpy = m_rightFrontVert.y * cosinus(m_matrixAngle) + m_rightFrontVert.x * sinus(m_matrixAngle);

	m_rightFrontVert.x = tmpx;
	m_rightFrontVert.y = tmpy;
	
	tmpx = m_rightMiddleVert.x * cosinus(m_matrixAngle) - m_rightMiddleVert.y * sinus(m_matrixAngle);
	tmpy = m_rightMiddleVert.y * cosinus(m_matrixAngle) + m_rightMiddleVert.x * sinus(m_matrixAngle);

	m_rightMiddleVert.x = tmpx; 
	m_rightMiddleVert.y = tmpy;
	
	tmpx = m_rightBottomVert.x * cosinus(m_matrixAngle) - m_rightBottomVert.y * sinus(m_matrixAngle);
	tmpy = m_rightBottomVert.y * cosinus(m_matrixAngle) + m_rightBottomVert.x * sinus(m_matrixAngle);
	
	m_rightBottomVert.x = tmpx; 
	m_rightBottomVert.y = tmpy;
	
	tmpx = m_leftBottomVert.x * cosinus(m_matrixAngle) - m_leftBottomVert.y * sinus(m_matrixAngle);
	tmpy = m_leftBottomVert.y * cosinus(m_matrixAngle) + m_leftBottomVert.x * sinus(m_matrixAngle);

	m_leftBottomVert.x = tmpx;
	m_leftBottomVert.y = tmpy;
}

void cTire::calcVertices(void) {
	int tLength= m_tireDiameter/2;
	int t2Length = m_tireWidth;
	
	int tXCoord = (double)tLength * sinus(m_angle);
	int tYCoord = -sqrt((double)(tLength*tLength)-(double)(tXCoord*tXCoord));
	
	int t2XCoord = t2Length * cosinus(m_angle+180);
	int t2YCoord = t2Length * sinus(m_angle+180);

	//left middle of the tire
	 m_leftMiddleVert.x = 0 + m_xpos;
	 m_leftMiddleVert.y = 0 + m_ypos;
	 //front left of tire
	 m_leftFrontVert.x = tXCoord + m_xpos;
	 m_leftFrontVert.y = tYCoord + m_ypos;
	 
	 //bottom left of tire
	 m_leftBottomVert.x = -tXCoord + m_xpos;
	 m_leftBottomVert.y = -tYCoord + m_ypos;

	//front right of tire
	m_rightFrontVert.x = (-1*(tYCoord/2)) + m_leftFrontVert.x;
	m_rightFrontVert.y = (tXCoord/2) +  m_leftFrontVert.y;

	//middle right of tire
	m_rightMiddleVert.x = (-1*(tYCoord/2)+tXCoord) - tXCoord + m_xpos;
	m_rightMiddleVert.y = ((tXCoord/2)+tYCoord) - tYCoord + m_ypos;
	
	//middle right of tire
	glVertex2i((-1*(tYCoord/2)+tXCoord)-tXCoord + m_xpos, ((tXCoord/2)+tYCoord)-tYCoord + m_ypos);
	//bottom right of tire
	glVertex2i((-1*(tYCoord/2)+tXCoord)-tXCoord-tXCoord + m_xpos, ((tXCoord/2)+tYCoord)-tYCoord-tYCoord + m_ypos);

	//bottom right of tire
	m_rightBottomVert.x = (-1*(tYCoord/2)+tXCoord)-tXCoord -tXCoord + m_xpos;
	m_rightBottomVert.y = ((tXCoord/2)+tYCoord)-tYCoord -tYCoord + m_ypos;

}

void cTire::drawTire(void) {
//	calcVertices();

	glBegin(GL_LINES);
	 glColor3f(1.0, 1.0, 1.0);

	glVertex2f(m_leftMiddleVert.x, m_leftMiddleVert.y);	
	glVertex2f(m_leftFrontVert.x, m_leftFrontVert.y);	

	glVertex2f(m_leftFrontVert.x, m_leftFrontVert.y);	
	glVertex2f(m_rightFrontVert.x, m_rightFrontVert.y);	
		
	glVertex2f(m_rightFrontVert.x, m_rightFrontVert.y);	
	glVertex2f(m_rightMiddleVert.x, m_rightMiddleVert.y);	

	glVertex2f(m_rightMiddleVert.x, m_rightMiddleVert.y);	
	glVertex2f(m_rightBottomVert.x, m_rightBottomVert.y);	

	glVertex2f(m_rightBottomVert.x, m_rightBottomVert.y);	
	glVertex2f(m_leftBottomVert.x, m_leftBottomVert.y);	

	glVertex2f(m_leftBottomVert.x, m_leftBottomVert.y);	
	glVertex2f(m_leftMiddleVert.x, m_leftMiddleVert.y);	

	glEnd();

}

void cTire::drawTire_obsolete(void) {
	int tLength=100;
	int tXCoord = (double)tLength * sinus(m_angle);
	int tYCoord = -sqrt((double)(tLength*tLength)-(double)(tXCoord*tXCoord));
	
	int t2Length = 50;
	int t2XCoord = t2Length * cosinus(m_angle+180);
	int t2YCoord = t2Length * sinus(m_angle+180);

	glBegin(GL_LINES);
	 glColor3f(1.0, 1.0, 1.0);
	//left middle of the tire
	 glVertex2i(0 + m_xpos, 0 + m_ypos);
	 //front left of tire
	 glVertex2i(tXCoord + m_xpos, tYCoord + m_ypos);

	//left middle of the tire
	 glVertex2i(0 + m_xpos, 0 + m_ypos);
	//bottom left of the tire
	 glVertex2i(-tXCoord + m_xpos, -tYCoord + m_ypos);

	//top left of tire
	 glVertex2i(tXCoord + m_xpos, tYCoord + m_ypos);
	 //top right of tire
	 glVertex2i(tXCoord - t2XCoord + m_xpos, tYCoord - t2YCoord + m_ypos);

	//bottom left of tire
	 glVertex2i(-tXCoord + m_xpos, -tYCoord + m_ypos);
	 //bottom right of tire
	 glVertex2i(-tXCoord - t2XCoord + m_xpos, -tYCoord - t2YCoord + m_ypos);


	 //bottom right of tire
	 glVertex2i(-tXCoord - t2XCoord + m_xpos, -tYCoord - t2YCoord + m_ypos);
	 //top right of tire
	 glVertex2i(tXCoord - t2XCoord + m_xpos, tYCoord - t2YCoord + m_ypos);
	glEnd();

}

void cTire::rotateTire(int degrees) {
	if (m_angle+degrees > -46 && m_angle+degrees < 46) m_angle += degrees;
}

double cTire::sinus(int degrees) {
	return (double)sin((double)degrees/180*3.1415926535);
}

double cTire::cosinus(int degrees) {
	return (double)cos((double)degrees/180*3.1415926535);
}

int cTire::getAngle() {
	return m_angle;
}
